//
//  ZJTabBar.m
//  YueLoveBaby
//
//  Created by 张敬 on 2018/4/21.
//  Copyright © 2018年 张敬. All rights reserved.
//

#import "TJTabBar.h"
#import "TJTabBarItem.h"

NSString *const  ZJTabBarScrollViewTopNotification = @"ZJTabBarScrollViewTopNotification";
NSString *const  ZJTabBarScrollViewDownNotification = @"ZJTabBarScrollViewDownNotification";

#define middleGap 70.f

@interface TJTabBar()

/** 当前选中的按钮 */
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *tabbarNomalImageArr;
@property (nonatomic, strong) NSArray *tabbarSelectedImageArr;
@property (nonatomic, strong) NSMutableArray *tabbarItems;

@property (nonatomic, strong) TJTabBarItem *firstItem;
@property (nonatomic, strong) TJTabBarItem *selectedItem;

@property (nonatomic, strong) UIButton *middleBtn;
@property (nonatomic, strong) UILabel *middleLab;
@property (nonatomic, strong) UIView *middleView;
@property (nonatomic, strong) UIImageView *circleIcon;


@end

@implementation TJTabBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        self.frame = CGRectMake(0, KScreenHeight - TJ_TabBarHeight_Defind, KScreenWidth, TJ_TabBarHeight_Defind);
        self.backgroundColor = KClearColor;
        
        [self addSubview:self.circleIcon];
        [self addSubview:self.middleView];
        [self addSubview:self.middleBtn];
        [self addSubview:self.middleLab];
        
        _middleLab.mj_y = _middleBtn.bottom + 4;
        [self configTabbar];
    }
    return self;
}


- (void)configTabbar
{
    float gap = (KScreenWidth - middleGap)/self.titleArr.count;
    for (int i = 0; i< self.titleArr.count; i++) {
        
        CGFloat midGap = 0.f;
        if (i >= self.titleArr.count/2) {
            midGap = middleGap;
        }
        
        CGRect frame = CGRectMake(i*gap + midGap, (TJ_TabBarHeight_Defind - TJ_TabBarHeight), gap, TJ_TabBarHeight);
        TJTabBarItem *item = [[TJTabBarItem alloc] initWithFrame:frame];
        item.tag = i;
        [self addSubview:item];
        
        [item setTabbarText:self.titleArr[i]];
        [item setTabbarIcon:self.tabbarNomalImageArr[i]];
        if (i == 0) {
            [item setTabbarIcon:self.tabbarSelectedImageArr[0]];
            item.selected = YES;
            item.tabbarTitleLayer.foregroundColor = mMainColor.CGColor;
            self.selectedItem = item;
        }
        [item addTarget:self action:@selector(didClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.tabbarItems addObject:item];
    }
    
    //
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
}


#pragma mark - 点击事件处理
- (void)didClicked:(TJTabBarItem *)sender
{
    self.selectedItem.selected = NO; // 上一个按钮取消选中
    [self.selectedItem setTabbarIcon:self.tabbarNomalImageArr[self.selectedItem.tag]];
    sender.selected = YES; // 当前按钮设置选中
    self.selectedItem.tabbarTitleLayer.foregroundColor = UIColorFromRGB(0x5C5C5C).CGColor;
    [sender setTabbarIcon:self.tabbarSelectedImageArr[sender.tag]];
    sender.tabbarTitleLayer.foregroundColor = mMainColor.CGColor;
    self.selectedItem = sender; // 当前按钮赋值给selectedBtn
    
    if (self.block) {
        self.block((int)sender.tag);
    }
    
   // NSLog(@"点击第%d个按钮",(int)sender.tag);
}


- (void)dealJumpTapOperationByIndex:(NSInteger)index{
    
    self.selectedItem.selected = NO; // 上一个按钮取消选中
    [self.selectedItem setTabbarIcon:self.tabbarNomalImageArr[self.selectedItem.tag]];
    TJTabBarItem *item = self.tabbarItems[index];
    item.selected = YES; // 当前按钮设置选中
    self.selectedItem.tabbarTitleLayer.foregroundColor = UIColorFromRGB(0x333333).CGColor;
    [item setTabbarIcon:self.tabbarSelectedImageArr[item.tag]];
    item.tabbarTitleLayer.foregroundColor = mMainColor.CGColor;
    self.selectedItem = item; // 当前按钮赋值给selectedBtn
}

- (void)didChickedMiddleBtn:(id)sender{
    
    if (self.middleBlock) {
        self.middleBlock();
    }
}

#pragma mark - setter and getter
- (NSArray *)titleArr
{
    if (!_titleArr) {
       _titleArr = @[@"养猪",@"肉市",@"发现",@"我的"];
    }
    return _titleArr;
}

- (NSArray *)tabbarNomalImageArr
{
    if (!_tabbarNomalImageArr) {
        UIImage *image1 = [UIImage imageNamed:@"tabbar_adoptpig"]  ;
        UIImage *image2 = [UIImage imageNamed:@"tabbar_meatmarket"];
        UIImage *image3 = [UIImage imageNamed:@"tabbar_discovery"]  ;
        UIImage *image4 = [UIImage imageNamed:@"tabbar_me"];
        _tabbarNomalImageArr = @[image1, image2, image3, image4];
    }
    return _tabbarNomalImageArr;
}


- (NSArray *)tabbarSelectedImageArr
{
    if (!_tabbarSelectedImageArr) {
        UIImage *image1 = [UIImage imageNamed:@"tabbar_adoptpig_selected"]  ;
        UIImage *image2 = [UIImage imageNamed:@"tabbar_meatmarket_selected"];
        UIImage *image3 = [UIImage imageNamed:@"tabbar_discovery_selected"]  ;
        UIImage *image4 = [UIImage imageNamed:@"tabbar_me_selected"];
        _tabbarSelectedImageArr = @[image1, image2, image3, image4];
    }
    return _tabbarSelectedImageArr;
}


- (UIButton *)middleBtn{
    if (!_middleBtn) {
        _middleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _middleBtn.backgroundColor = KClearColor;
        _middleBtn.frame = CGRectMake((KScreenWidth - 54)/2.0, 0, 54, 54);
        [_middleBtn setImage:[UIImage imageNamed:@"nav_meat_center"] forState:UIControlStateNormal];
        [_middleBtn addTarget:self action:@selector(didChickedMiddleBtn:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _middleBtn;
}


- (UILabel *)middleLab{
    if (!_middleLab) {
        
        _middleLab = [UILabel makeLabelWithText:@"看看" textColor:mMainColor font:mMediumFont(11) textAliment:1];
                      
        _middleLab.frame = CGRectMake((KScreenWidth - middleGap)/2.0, 0, middleGap, 15);

    }
    return _middleLab;
}


- (UIView *)middleView{
    if (!_middleView) {
        _middleView = [UIView new];
        _middleView.backgroundColor = KWhiteColor;
        _middleView.size = CGSizeMake(KScreenWidth, TJ_TabBarHeight_Defind - 22);
        _middleView.mj_x = 0;
        _middleView.mj_y = 22;
        
        CGFloat gap = 0.5;
        CAShapeLayer *lineL = [CAShapeLayer layer];
        lineL.fillColor = mIconLineColor.CGColor;
        UIBezierPath *pathL = [UIBezierPath bezierPathWithRect:CGRectMake(0, -gap, KScreenWidth, gap)];
        lineL.path = pathL.CGPath;
        [_middleView.layer addSublayer:lineL];


        
    }
    return _middleView;
}


- (UIImageView *)circleIcon{
    if (!_circleIcon) {
        UIImage *image = [UIImage imageNamed:@"circle"];
        _circleIcon = [[UIImageView alloc] init];
        _circleIcon.image = image;
        _circleIcon.size = CGSizeMake(middleGap, 22);
        _circleIcon.mj_y = 0;
        _circleIcon.mj_x = (KScreenWidth - _circleIcon.width)/2.0;
        _circleIcon.userInteractionEnabled = YES;
    }
    return _circleIcon;
}


- (NSMutableArray *)tabbarItems{
    if (!_tabbarItems) {
        _tabbarItems = [NSMutableArray array];
    }
    return _tabbarItems;
}

@end
